package com.hskn.hss.module.sysinfo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.permission.mapper.SysPermissionMapper;
import com.hskn.hss.module.sysinfo.entity.SysInfo;
import com.hskn.hss.module.sysinfo.mapper.SysInfoMapper;
import com.hskn.hss.module.sysinfo.service.ISysInfoService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 系统配置管理 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Slf4j
@RestController
@RequestMapping("/hss/sysinfo")
public class SysInfoController {

    @Autowired
    private ISysInfoService sysInfoService;

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_info", idKey = "sysId", bean = SysInfo.class, beanMapper = SysInfoMapper.class, names = "sysName, homeName, comName")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            sysInfoService.mUpdate(params);
            return AjaxResult.success("修改成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "detail")
    public AjaxResult<Object> detail(HttpServletRequest request) {
        try {
            SysInfo sysInfo = sysInfoService.getById(1);
            return AjaxResult.success(sysInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 获取列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public AjaxResult<Object> getList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject json = sysInfoService.getList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }
}

